﻿
// ToolDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "Tool.h"
#include "ToolDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



void StrToHex(byte* pbDest, char* pszSrc, int nLen)
{
	char h1, h2;
	byte s1, s2;
	for (int i = 0; i < nLen; i++)
	{
		h1 = pszSrc[2 * i];
		h2 = pszSrc[2 * i + 1];

		s1 = toupper(h1) - 0x30;
		if (s1 > 9)
			s1 -= 7;

		s2 = toupper(h2) - 0x30;
		if (s2 > 9)
			s2 -= 7;

		pbDest[i] = s1 * 16 + s2;
	}
}


void HexToStr(char* pszDest, byte* pbSrc, int nLen)
{
	char ddl, ddh;
	for (int i = 0; i < nLen; i++)
	{
		ddh = 48 + pbSrc[i] / 16;
		ddl = 48 + pbSrc[i] % 16;
		if (ddh > 57) ddh = ddh + 7;
		if (ddl > 57) ddl = ddl + 7;
		pszDest[i * 3] = ddh;
		pszDest[i * 3 + 1] = ddl;
		pszDest[i * 3 + 2] = 0x20;
	}


}


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CToolDlg 对话框



CToolDlg::CToolDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_TOOL_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CToolDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CToolDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CToolDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CToolDlg::OnBnClickedButton3)

END_MESSAGE_MAP()


// CToolDlg 消息处理程序

BOOL CToolDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	SetDlgItemText(IDC_EDIT4, "0x33");
	SetDlgItemText(IDC_EDIT5, "0x33");

	((CEdit*)GetDlgItem(IDC_EDIT4))->SetLimitText(2);
	((CEdit*)GetDlgItem(IDC_EDIT5))->SetLimitText(2);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CToolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CToolDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CToolDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CToolDlg::OnBnClickedButton1()
{
	CString text;
	GetDlgItemText(IDC_EDIT1, text);
	if (text.IsEmpty())
	{
		MessageBox("请输入十六进制文本字符串后再试！", "提示");
		return;
	}
	text.Replace(" ", "");
	text.Replace("0x", "");
	text.Replace(",", "");
	CString str;
	str.Format("[%d]字节", text.GetLength() / 2);
	SetDlgItemText(IDC_EDIT6, str);
	char* tmp = new char[text.GetLength()];
	memset(tmp, 0, text.GetLength());
	StrToHex((byte*)tmp,text.GetBuffer(), text.GetLength()/2);

	byte sum = 0;
	int index = 0;
	while (index < text.GetLength() / 2)
	{
		sum += (byte)tmp[index];
		index++;
	}
	CString result;
	result.Format("%02X", sum);
	SetDlgItemText(IDC_EDIT3, result);
	delete[] tmp;
}

//逐字节加 -> 结果仍为十六进制字符串
void CToolDlg::OnBnClickedButton2()
{
	CString text;
	GetDlgItemText(IDC_EDIT1, text);
	if (text.IsEmpty())
	{
		MessageBox("请输入十六进制数字后再试！", "提示");
		return;
	}
	text.Replace(" ", "");
	text.Replace("0x", "");
	text.Replace(",", "");
	CString str;
	str.Format("[%d]字节", text.GetLength() / 2);
	SetDlgItemText(IDC_EDIT6, str);

	byte* tmp = new byte[text.GetLength()];
	memset(tmp, 0, text.GetLength());
	StrToHex(tmp, text.GetBuffer(), text.GetLength() / 2);

	CString data;
	GetDlgItemText(IDC_EDIT4, data);
	if (data.IsEmpty())
	{
		MessageBox("请输入需要逐字节加的十六进制字节后再试！", "提示");
		return;
	}
	data.Replace(" ", "");
	data.Replace("0x", "");
	data.Replace(",", "");
	byte b = 0;
	StrToHex(&b, data.GetBuffer(), 1);
	if (b == 0)
	{
		MessageBox("请输入需要逐字节加的非0十六进制字节后再试！", "提示");
		return;
	}
	
	for (int i = 0; i < text.GetLength() / 2; i++)
	{
		tmp[i] += b;
	}
	char* result = new char[2 * text.GetLength()+1];
	memset(result, 0, 2 * text.GetLength() + 1);
	HexToStr(result,tmp, text.GetLength() / 2);
	SetDlgItemText(IDC_EDIT2, CString(result, text.GetLength()  * 3));
	delete[] result;
}


void CToolDlg::OnBnClickedButton3()
{
	CString text;
	GetDlgItemText(IDC_EDIT1, text);
	if (text.IsEmpty())
	{
		MessageBox("请输入十六进制数字后再试！", "提示");
		return;
	}
	text.Replace(" ", "");
	text.Replace("0x", "");
	text.Replace(",", "");
	CString str;
	str.Format("[%d]字节", text.GetLength() / 2);
	SetDlgItemText(IDC_EDIT6, str);
	byte* tmp = new byte[text.GetLength()];
	memset(tmp, 0, text.GetLength());
	StrToHex(tmp, text.GetBuffer(), text.GetLength() / 2);

	CString data;
	GetDlgItemText(IDC_EDIT4, data);
	if (data.IsEmpty())
	{
		MessageBox("请输入需要逐字节加的十六进制字节后再试！", "提示");
		return;
	}
	data.Replace(" ", "");
	data.Replace("0x", "");
	data.Replace(",", "");
	byte b = 0;
	StrToHex(&b, data.GetBuffer(), 1);
	if (b == 0)
	{
		MessageBox("请输入需要逐字节加的非0十六进制字节后再试！", "提示");
		return;
	}

	for (int i = 0; i < text.GetLength() / 2; i++)
	{
		tmp[i] -= b;
	}
	char* result = new char[2 * text.GetLength() + 1];
	memset(result, 0, 2 * text.GetLength() + 1);
	HexToStr(result, tmp, text.GetLength() / 2);
	SetDlgItemText(IDC_EDIT2, CString(result, text.GetLength() * 3));
	delete[] result;
}


